#include "prs_struct.h"
#include "str_utils.h"
#include "utils.h"

pprs_struct prs_struct_init()
{
    pprs_struct res = (pprs_struct)xmalloc(sizeof(prs_struct));
    res->argv = NULL;
    res->argc = res->__alloc_cnt = 0;
    res->iseof = 0;
    return res;
}

void prs_struct_free(pprs_struct data)
{
    if (data == NULL)
        return;
    if (data->argc)
        for (size_t i = 0; i < data->argc; i++)
            if (data->argv[i])
                free(data->argv[i]);
    if (data->argv)
        free(data->argv);
    free(data);
}

void prs_struct_add(pprs_struct data, char *expr)
{
    if (data == NULL)
        return;
    data->argc++;
    if (data->argc > data->__alloc_cnt)
    {
        data->__alloc_cnt = (data->argv != NULL) ? data->__alloc_cnt * 2: 1;
        data->argv = (char **)xrealloc(data->argv, sizeof(char *) * data->__alloc_cnt);
    }
    data->argv[data->argc - 1] = (expr != NULL) ? strdup(expr) : NULL;
}


void prs_struct_reserve(pprs_struct data, size_t new_size)
{
    if (data == NULL)
        return;
    data->argv = (char **)xrealloc(data->argv, new_size * sizeof(char *));
    data->__alloc_cnt = new_size;
    if (data->argc > new_size)
        data->argc = new_size;
}
